﻿using Caliburn.Micro;
using Phones.Client.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace Phones.Client.ViewModel
{
    [Export(typeof(ServiceNodePropertiesViewModel))]
    public class ServiceNodePropertiesViewModel : Screen
    {
        private ServiceNode _item;

        [ImportingConstructor]
        public ServiceNodePropertiesViewModel(ServiceNode item)
        {
            _item = item;
            DisplayName = "Правка сервизного узла";
        }

        public string Name
        {
            get 
            { 
                return _item == null ? String.Empty : _item.Name;
            }
            set
            {
                if (_item == null) _item = new ServiceNode();
                _item.Name = value;
                NotifyOfPropertyChange(() => Name);
            }
        }

        public void Save()
        {
            if (Validated())
            {
                TryClose(true);
            }
        }

        public bool CanSave
        {
            get { return true; }
        }

        public void Cancel()
        {
            TryClose(false);
        }

        private bool Validated()
        {
            return _item != null && !String.IsNullOrEmpty(_item.Name);
        }
    }
}
